const fileType = {
  "pdf":{
      type:"application/pdf;charset=utf-8",// 文件转换类型
      base64Type:"data:application/pdf;base64,",// base64前缀
  },
  "img":{
      // img适用所有通用图片格式
      type:"image/png",
      base64Type:"data:image/png;base64,",
  },
  "gif":{
      type:"image/gif",
      base64Type:"data:image/gif;base64,",
  },
  "doc":{
      type:"application/msword",
      base64Type:"data:application/msword;base64,"
  },
  "docx":{
      type:"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
      base64Type:"data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,"
  },
  "xls":{
      type:"application/vnd.ms-excel",
      base64Type:"data:application/vnd.ms-excel;base64,"
  },
  "xlsx":{
      type:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
      base64Type:"data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,"
  },
  "ppt":{
      type:"application/vnd.ms-powerpoint",
      base64Type:"data:application/vnd.ms-powerpoint;base64,"
  },
  "pptx":{
      type:"application/vnd.openxmlformats-officedocument.presentationml.presentation",
      base64Type:"data:application/vnd.openxmlformats-officedocument.presentationml.presentation;base64,"
  },
  "txt":{
      type:"text/plain",
      base64Type:"data:text/plain;base64,"
  },
};
let files,types,changeTypes;

// file 附件
// type 文件类型(img,pdf)
// changeType 附件转换类型(base64 blob url),三者互转(列如:base64_url)
export async function changeUrl(file,type,changeType) { 
  if(!file || !type || !changeType || !fileFun[changeType]) {
      return file;
  }
  files = file;
  types = type;
  changeTypes = changeType;
  const fileWj = await fileFun[changeType](file);
  return fileWj;
}
function base64_url(file){
  // base64转url
  const blob = base64_blob(file);
  return window.URL.createObjectURL(blob);
}
function base64_blob(file){
  // base64转blob
  const arr = file.split(",");
  const mime = arr[0].match(/:(.*?);/)[1];
  const bstr = atob(arr[1]);
  let n = bstr.length;
  const u8arr = new Uint8Array(n);
  while (n--) {
      u8arr[n] = bstr.charCodeAt(n);
  }
  return new Blob([u8arr], { type: mime });
}
function blob_url(file){
   // blob转url
  const blob = new Blob([file], {type: fileType[types].type});
  return URL.createObjectURL(blob);
}
function blob_base64(file){
  // blob转base64
  return new Promise((resolve) => {
      const reader = new FileReader();
      reader.readAsDataURL(file);
      reader.onload = () => {
          const arr = reader.result.split(",");
          resolve(fileType[types].base64Type + arr[1]);
      };
  });
}
async function url_blob(url) {
  // url转blob
  return new Promise((resolve) => {
      const xhr = new XMLHttpRequest();
      xhr.open('GET',url,true);
      xhr.responseType = 'blob';
      xhr.onload = function(e) {
          const myBlob = this.response;
          // let files = new window.File([myBlob], myBlob.type, {type:myBlob.type}) // myBlob.type 自定义文件名
          resolve(myBlob);
      };
      xhr.send();
  })
}
async function url_base64(url){
  // url转base64
  const blob = await url_blob(url);
  return new Promise((resolve) => {
      blob_base64(blob).then(res => {
          resolve(res);
      });
  })
}

const fileFun = {
  "base64_url":base64_url,
  "blob_url":blob_url,
  "base64_blob":base64_blob,
  "blob_base64":blob_base64,
  "url_blob":url_blob,
  "url_base64":url_base64,
}